package com.yuand.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author yuandong
 * @date 2021/8/6 11:40
 */
@Controller
public class IndexController {


    /**
     * 进入登录页面
     * @return
     */
    @RequestMapping("/login.html")
    public String toLogin() {
        return "login";
    }


    /**
     * 登录提交页面
     * @param userName
     * @param password
     * @param mmp
     * @return
     */
    @RequestMapping("/user/login")
    public String login(String userName, String password, ModelMap mmp) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

        try {
            subject.login(token);   //登录成功进入首页
            return "index";
        } catch (UnknownAccountException e) {  //用户名不存在
            mmp.put("msg", "用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException e) {  //密码错误
            mmp.put("msg", "密码错误");
            return "login";
        }
    }


    /**
     * 进入列表页面
     * @return
     */
    @RequestMapping("/user/list")
    @RequiresPermissions("sys:user:list")
    public String list(){
        return "user/list";
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/user/add")
   // @RequiresPermissions("sys:user:add")
    public String add(){
        return "user/add";
    }


    /**
     * 进入编辑页面
     * @return
     */
    @RequestMapping("/user/edit")
    //@RequiresPermissions("sys:user:edit")
    public String edit(){
        System.out.println("进入修改页面...");
        return "user/edit";
    }

    /**
     * 进入删除页面
     * @return
     */
    @RequestMapping("/user/del")
    @RequiresPermissions("sys:user:del")
    public String del(){
        System.out.println("进入删除页面...");
        return "user/del";
    }

    /**
     * 权限不足页面
     * @return
     */
    @RequestMapping("/lesspermission.html")
    public String lessPermission(){
        return "lesspermission";
    }

}
